package com.sc.nft.admin.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.*;
import com.sc.nft.dto.CollectiveSalesConfigDTO;
import com.sc.nft.dto.CollectiveSalesPageDTO;
import com.sc.nft.dto.CollectiveSalesTimePointsConfigDTO;
import com.sc.nft.dto.CollectiveSalesVO;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.TmpCollectionVO;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.helper.MetaChainHepler;
import com.sc.nft.mapper.CollectiveOrderMapper;
import com.sc.nft.mapper.CollectiveSalesMapper;
import com.sc.nft.mapper.UserCollectionMapper;
import com.sc.nft.service.CollectiveSalesConfigService;
import com.sc.nft.service.UserCollectionService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/collective")
    @Api(tags = "后台集售接口")
public class CollectiveSalesAdminController {

    private final CollectiveSalesConfigService collectiveSalesConfigService;
    private final CollectiveSalesConfigDao collectiveSalesConfigDao;
    private final CollectiveSalesTimePointsDao collectiveSalesTimePointsDao;
    private final CollectiveSalesMapper collectiveSalesMapper;
    private final CollectiveOrderDao collectiveOrderDao;

    @PostMapping(value = "/intoPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "投入记录", notes = "投入记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<CollectiveSalesVO> intoPage(@Valid @RequestBody CollectiveIntoPageRequest request) {
        Page<CollectiveSalesVO> adminPage = collectiveSalesMapper.getAdminPage(request.toMp(), request.getFirstTitle(), request.getUserTel(), request.getStatus() != null ? request.getStatus().getCode() : 0, request.getStartTime(), request.getEndTime());
        for (CollectiveSalesVO record : adminPage.getRecords()) {
            if(record.getStatus()==ConSignStatus.SUCCESS){
                CollectiveOrder order = collectiveOrderDao.getPaySuccessBySalesId(record.getId());
                record.setGas(order.getGas().toPlainString());
                record.setCharge(order.getCharge().toPlainString());
                record.setBuyPrice(order.getBuyPrice().toPlainString());
                record.setRealReceiveAmount(order.getRealReceiveAmount().toPlainString());
            }
        }
        return PageResult.success(adminPage);
    }


    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加集售规则", notes = "添加集售规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result add(@Valid @RequestBody CollectiveSalesConfigDTO request) {
        collectiveSalesConfigService.crateCollectiveSalesConfigs(request);
        return Result.success();
    }

    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑集售规则", notes = "编辑集售规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result edit(@Valid @RequestBody CollectiveSalesConfigDTO request) {
        collectiveSalesConfigService.editCollectiveSalesConfigs(request);
        return Result.success();
    }
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "集售列表", notes = "集售列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<CollectiveSalesPageDTO> page(@Valid @RequestBody CollectiveRequest request) {
        return PageResult.success(collectiveSalesConfigService.collectivePage(request.toMp(),request.getCollectionId(),request.getStatus()));
    }

    @PostMapping(value = "/openStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "状态开启", notes = "状态开启", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result openStatus(@Valid @RequestBody IdRequest request) {
        CollectiveSalesConfig collectiveSalesConfig = collectiveSalesConfigDao.getById(request.getId());
        if(collectiveSalesConfig == null) {
            return Result.fail("未找到该配置");
        }
        collectiveSalesConfig.setStatus(true);
        collectiveSalesConfig.updateById();
        return Result.success();
    }

    @PostMapping(value = "/closeStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "关闭开启", notes = "关闭开启", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result closeStatus(@Valid @RequestBody IdRequest request) {
        CollectiveSalesConfig collectiveSalesConfig = collectiveSalesConfigDao.getById(request.getId());
        if(collectiveSalesConfig == null) {
            return Result.fail("未找到该配置");
        }
        collectiveSalesConfig.setStatus(false);
        collectiveSalesConfig.updateById();
        return Result.success();
    }


    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看详情", notes = "查看详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<CollectiveSalesConfigDTO> detail(@Valid @RequestBody IdRequest request) {
        CollectiveSalesConfig collectiveSalesConfig = collectiveSalesConfigDao.getById(request.getId());
        if(collectiveSalesConfig == null) {
            return Result.fail("未找到该配置");
        }
        CollectiveSalesConfigDTO collectiveSalesConfigDTO = new CollectiveSalesConfigDTO();
        collectiveSalesConfigDTO.setId(collectiveSalesConfig.getId());
        collectiveSalesConfigDTO.setCollectionId(collectiveSalesConfig.getCollectionId());
        collectiveSalesConfigDTO.setSort(collectiveSalesConfig.getSort());
        collectiveSalesConfigDTO.setEndIntoTime(collectiveSalesConfig.getEndIntoTime());
        collectiveSalesConfigDTO.setOpenLimit(collectiveSalesConfig.getOpenLimit());
        collectiveSalesConfigDTO.setRuleImage(collectiveSalesConfig.getRuleImage());
        collectiveSalesConfigDTO.setActivityEndTime(collectiveSalesConfig.getActivityEndTime());
        collectiveSalesConfigDTO.setServiceFeeRatio(collectiveSalesConfig.getServiceFeeRatio());
        collectiveSalesConfigDTO.setStartIntoTime(collectiveSalesConfig.getStartIntoTime());
        collectiveSalesConfigDTO.setIntoCollectionId(collectiveSalesConfig.getIntoCollectionId());
        collectiveSalesConfigDTO.setStatus(collectiveSalesConfig.getSort());
        collectiveSalesConfigDTO.setSumIntoLimit(collectiveSalesConfig.getSumIntoLimit());
        collectiveSalesConfigDTO.setGas(collectiveSalesConfig.getGas());
        List<CollectiveSalesTimePointsConfigDTO> timePointsConfigs = Lists.newArrayList();
        List<CollectiveSalesTimePoints> timePoints = collectiveSalesTimePointsDao.findByCollectiveId(collectiveSalesConfig.getId());
        for (CollectiveSalesTimePoints timePoint : timePoints) {
            CollectiveSalesTimePointsConfigDTO collectiveSalesTimePointsConfigDTO = new CollectiveSalesTimePointsConfigDTO();
            collectiveSalesTimePointsConfigDTO.setBuyCollectionId(timePoint.getBuyCollectionId());
            collectiveSalesTimePointsConfigDTO.setTimePoint(timePoint.getTimePoint());
            collectiveSalesTimePointsConfigDTO.setPrice(timePoint.getPrice());
            timePointsConfigs.add(collectiveSalesTimePointsConfigDTO);
        }
        collectiveSalesConfigDTO.setTimePointsConfigs(timePointsConfigs);
        return Result.success(collectiveSalesConfigDTO);
    }

}
